#!/bin/bash

source $DRAW_INI
source $DRAW_CFG

# Check for intervals file
if [ ! -s "${1}.indel_calls.interval_list" ];then
  echo "Error: Missing interval file: " ${1}.indel_calls.interval_list " from realignIndelS1"
  exit 100
fi

java -Xmx5g -jar $GATK \
   -T IndelRealigner \
   -R $REF_FASTA \
   -I ${1}.bam \
   -targetIntervals ${1}.indel_calls.interval_list \
   -o ${1}.indel_realign.bam \
   --bam_compression 7 \
   --maxReadsInMemory 200000 \
   -known $DBSNP_REF_INDELS

EXITSTATUS=$?

#force error when missing output
if [ ! -s "${1}.indel_realign.bam" ]
then
 echo "Missing output BAM ${1}.indel_realign.bam"
 exit 100
fi

# Check BAM EOF
BAM_28=$(tail -c 28 ${1}.indel_realign.bam|xxd -p)
if [ "$MAGIC28" != "$BAM_28" ]
then
  echo "Error with BAM EOF" 1>&2
  exit 100
fi

exit $EXITSTATUS

